import React from 'react';
import PropTypes from 'prop-types';
import {Modal} from 'antd';
import {formatMessage} from './intl';
import ExcelImportIntl from '../common/ExcelImportIntl';
import styles from './style.css';

const basePath = fetch.basePath || '';

class ImportPanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
        };
    }
    afterUpload = file => {
        if(file.ok)
            this.props.onImportChange(file.response);
    }
    render() {
        const data = {
            brandId: this.props.brandId,
            dealerId: this.props.dealerId,
        };
        return (
            <Modal
                title={formatMessage({
                    id: 'importPanel.show.importStock',
                    defaultMessage: '导入清单'
                })}
                width="60%"
                maskClosable={false}
                footer={null}
                destroyOnClose={true}
                wrapClassName={styles.dataSelectModal}
                visible={true}
                onCancel={this.props.onCancel}>
                <ExcelImportIntl
                    action={`${basePath}/sales/api/v1/dealerResourceLists/import`}
                    template={this.props.importTemp}
                    data={data}
                    templateName={formatMessage({
                        id: 'importPanel.show.importPart',
                        defaultMessage: '导入服务站可供资源清单模板'
                    })}
                    afterUpload={this.afterUpload} />
            </Modal >
        );
    }
}

ImportPanel.propTypes = {
    onImportChange: PropTypes.func.isRequired,
    brandId: PropTypes.string,
    dealerId: PropTypes.string,
    history: PropTypes.object,
    importTemp: PropTypes.func,
    transportType: PropTypes.number,
    onCancel: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    importTemp
} from './actions';
const mapDispatchToProps = dispatch => ({
    importTemp: () => dispatch(importTemp())
});
export default connect(null, mapDispatchToProps)(ImportPanel);
